home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / tnos / tnos100s / color.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-27  |  4.1 KB  |  267 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. #ifndef DEMO
  5. #undef DEMO
  6. #endif
  7.  
  8. #ifdef DEMO
  9. #define tprintf printf
  10. #define tputc putchar
  11.  
  12. static unsigned char last[2];
  13.  
  14. void
  15. tflush ()
  16. {
  17.     fflush (stdout);
  18. }
  19. #else
  20. #include "usock.h"
  21.  
  22. void
  23. myusflush (s)
  24. int s;
  25. {
  26. register struct usock *up;
  27. struct mbuf *bp;
  28.  
  29.     if((up = itop(s)) == NULLUSOCK)
  30.         return;
  31.  
  32.     if(up->obuf == NULLBUF)
  33.         return;
  34.     if (len_p (up->obuf) < 220)
  35.         return;
  36.     usflush (s);
  37. }
  38. #endif
  39.  
  40.  
  41. int
  42. colorprintf (escapeflag, usecolor, str)
  43. char *escapeflag, usecolor;
  44. unsigned char *str;
  45. {
  46. unsigned char c;
  47. int foundone = 0;
  48.  
  49.  
  50.     while ((c = *str++) != 0)    {
  51.         if (c == 0x1b)    {    /* escape sequence */
  52.             if (usecolor)    {
  53.                 foundone = 1;
  54. #ifdef DEMO
  55.                 tflush ();
  56. #else
  57.                 myusflush(Curproc->output);
  58. #endif
  59.             }
  60.             if (escapeflag)
  61.                 *escapeflag = 1;
  62.         }
  63.         else if (escapeflag && *escapeflag)    {
  64.             if (!isdigit(c) && c != ';' && c != '[')
  65.                 *escapeflag = 0;
  66.         }
  67.         if (!escapeflag || (escapeflag && !*escapeflag) || usecolor)    {
  68.             tputc (c);
  69.         }
  70.     }
  71.     return (foundone);
  72. }
  73.  
  74.  
  75. static int
  76. getcolor (color)
  77. unsigned char color;
  78. {
  79. int retval = 30;
  80.  
  81.     switch (color)    {
  82.         case '1': case '9':    retval += 4;    break;
  83.         case '2': case 'A':    retval += 2;    break;
  84.         case '3': case 'B':    retval += 6;    break;
  85.         case '4': case 'C':    retval += 1;    break;
  86.         case '5': case 'D':    retval += 5;    break;
  87.         case '6': case 'E':    retval += 3;    break;
  88.         case '7': case 'F':    retval += 7;
  89.         default:        break;
  90.     }
  91.     return (retval);
  92. }
  93.  
  94.  
  95. static int
  96. getone (fp)
  97. FILE *fp;
  98. {
  99. int c;
  100.  
  101.     c = getc (fp);
  102.     if (feof (fp))
  103.         c = 0;
  104.     return (c);
  105. }
  106.  
  107.  
  108. static void
  109. getkeyword (input, fp)
  110. unsigned char *input;
  111. FILE *fp;
  112. {
  113. unsigned char c;
  114. int k;
  115.  
  116.     do    {
  117.         for (k = 0; k < 15; k++)    {
  118.             input[k] = c = getone (fp);
  119.             if (!c || (c == '@'))
  120.                 break;
  121.         }
  122.         input[k] = 0;
  123.         if (!c)
  124.             k++;
  125.     } while (!k);
  126. }
  127.  
  128.  
  129. void
  130. colorcls ()
  131. {
  132. #ifdef DEMO
  133.     tflush ();
  134. #else
  135.     myusflush(Curproc->output);
  136. #endif
  137.     tprintf ("\x1b[2J");
  138. }
  139.  
  140.  
  141. static int
  142. cmdmatch (input)
  143. unsigned char *input;
  144. {
  145. int retval = 0;
  146.  
  147.     if (!strcmp ("CLS", input))    {
  148.         colorcls();
  149.         retval = 1;
  150.     }
  151.     return (retval);
  152. }
  153.  
  154.  
  155. static int
  156. blinking (c)
  157. unsigned char c;
  158. {
  159.     return ((c > '7') ? 1 : 0);
  160. }
  161.  
  162.  
  163. static int
  164. bold (c)
  165. unsigned char c;
  166. {
  167.     return ((c > '7') ? 1 : 0);
  168. }
  169.  
  170.  
  171. int
  172. colorchange (input, last)
  173. register char *input, *last;
  174. {
  175. int retval = 0, putone = 0, temp, putall = 0;
  176.  
  177.  
  178.     if (!strncmp (input, last, 2))
  179.         return (1);
  180.     if ((strlen(input) == 2) && (isxdigit (input[0])) && (isxdigit (input[1])))    {
  181. #ifdef DEMO
  182.         tflush ();
  183. #else
  184.         myusflush(Curproc->output);
  185. #endif
  186.         tprintf ("\x1b[");
  187.         retval = 2;
  188.         if (!*last)
  189.             putall++;
  190.         if (putall || (blinking(*last) && !blinking(*input)) || (bold (last[1]) && !bold(input[1]))) {
  191.             tputc ('0');
  192.             retval++;
  193.             putone++;
  194.             putall++;
  195.         }
  196.         if (blinking(*input))    {
  197.             tprintf ("%s5", putone ? ";" : "");
  198.             putone++;
  199.             retval++;
  200.         }
  201.         temp = getcolor(*input);
  202.         if (putall || temp != getcolor (*last))        {
  203.             tprintf ("%s%d", putone ? ";" : "", temp + 10);
  204.             retval += (2 + putone);
  205.             putone++;
  206.         }
  207.         if (putall || (input[1] != last[1]))    {
  208.             tprintf ("%s%s%d", putone ? ";" : "", (input[1] > '7') ? "1;" : "",
  209.                 getcolor (input[1]));
  210.             retval += (putone + 2 + ((input[1] > 7) ? 2 : 0));
  211.         }
  212.         tputc ('m');
  213.         retval += 1;
  214.         memcpy (last, input, 2);
  215.     }
  216.     return (retval);
  217. }
  218.  
  219.  
  220. void
  221. colorfile (filenm, last)
  222. char *filenm, *last;
  223. {
  224. FILE *fp;
  225. unsigned char c, input[16];
  226. char invalid = 0;
  227.  
  228.     if (!(fp = fopen (filenm, "rb")))
  229.         return;
  230.     while (!feof (fp))    {
  231.         if (!(c = getone (fp)))
  232.             continue;
  233.         if (c == 0x1b)
  234.             invalid = 1;
  235.         if (c == '@')    {
  236.             invalid = 0;
  237.             getkeyword (input, fp);
  238.             if (!colorchange (input, last))
  239.                 if (!cmdmatch (input))
  240.                     tprintf ("@%s@", input);
  241.         } else
  242. #ifndef DEMO
  243.             if ((c != 0x0d) && (c != 0x1a))
  244. #endif
  245.                 tputc (c);
  246.     }
  247.     fclose (fp);
  248.     if (invalid)
  249.         last[0] = 0;
  250. }
  251.  
  252.  
  253.  
  254. #ifdef DEMO
  255. void
  256. main (argc, argv)
  257. int argc;
  258. char *argv[];
  259. {
  260.     last[0] = last[1] = 0;
  261.     if (argc == 1)
  262.         exit (0);
  263.     colorfile (argv[1], last);
  264. }
  265. #endif
  266.  
  267.